setwd("../")
rm(list = ls())

# # only need to install once
# devtools::install_github("cloudyr/pyMTurkR", dependencies=TRUE)
# devtools::install_github("Luwei-Ying/validateIt", dependencies=TRUE)
# load R package
library(validateIt)

### load documents with predicted topics
top1domText <- read.csv("textpool/top1domText.csv", stringsAsFactors = FALSE)

OLbroadcareful <- validateLabel(type = "OL", n = 500, 
                             text.predict = top1domText, text.name = "post_text",
                             labels = c("Equal Pay for Women",
                                        "Healthcare/Reproductive Rights",
                                        "Agriculture",
                                        "Student Loan/Debt",
                                        "Drug Abuse",
                                        "Higher Education/Job Training",
                                        "Wall Street/Financial Sector",
                                        "Government Sutdown/Congressional Budget",
                                        "Obamacare/Tax Policy",
                                        "Deficits/Debt/Budget"), 
                             labels.index = c(1, 9, 21, 35, 44, 62, 65, 70, 88, 89), 
                             labels.add = c("International Trade",
                                            "Praising Active Military/Military Units",
                                            "Terrorism",
                                            "Military Sexual Assault",
                                            "Nuclear Deterrance/International Security",
                                            "Air Force",
                                            "Honoring Specific Veterans",
                                            "Honoring Veterans/Heroes",
                                            "Military Operations/Armed Conflicts",
                                            "Veterans Affairs/Veterans Healthcare"))

goldOL <- read.csv("goldstandard/goldOL.csv", stringsAsFactors = FALSE)
OLbroadcareful <- mixGold(tasks = OLbroadcareful, golds = goldOL)
OLbroadcareful <- recordTasks(type = "OL", tasks = OLbroadcareful,
                              path = "labelrecords/OLbroadcareful.Rdata")

# ##### interact with Mturk from here #####
# ### environment
# library(pyMTurkR)
# Sys.setenv(AWS_ACCESS_KEY_ID = "AWS_ACCESS_KEY_ID")
# Sys.setenv(AWS_SECRET_ACCESS_KEY = "AWS_SECRET_ACCESS_KEY")
# options(pyMTurkR.sandbox = T) # Change sandbox = F when ready to run on MTurk
# AccountBalance()
# 
# ### send tasks (first trial)
# OLbroadcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLbroadcareful, HITidspath = "HITids/OLbroadcarefulids1.Rdata")
# ### extend HITs if needed
# for(i in OLbroadcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLbroadcarefulids1.Rdata")
# OLbroadcarefulresults1 <- getResults(batch_id = "OLbroadcareful1", hit_ids = HITids, retry = FALSE)
# write.csv(OLbroadcarefulresults1, "OL/results/OLbroadcarefulresults1.csv", row.names = FALSE)
# evalResults(results = OLbroadcarefulresults1, key = OLbroadcareful, type = "OL")
# 
# ### send tasks (second trial)
# OLbroadcarefulids <- sendTasks(hit_type = 'find_from_dashboard', hit_layout = 'find_from_dashboard', type = "OL",
#                                tasksrecord = OLbroadcareful, HITidspath = "HITids/OLbroadcarefulids2.Rdata")
# ### extend HITs if needed
# for(i in OLbroadcarefulids[[1]]){
#   ExtendHIT(hit = i, add.seconds = 60*60*26)
# }
# 
# ### get results
# load("HITids/OLbroadcarefulids2.Rdata")
# OLbroadcarefulresults2 <- getResults(batch_id = "OLbroadcareful2", hit_ids = HITids, retry = FALSE)
# write.csv(OLbroadcarefulresults2, "OL/results/OLbroadcarefulresults2.csv", row.names = FALSE)
# evalResults(results = OLbroadcarefulresults2, key = OLbroadcareful, type = "OL")